class TurfClass {
  static CheckIntersect(polygon1, polygon2) {
    return turf.booleanIntersects(polygon1, polygon2);
  }
  static CheckContains(polygon1, polygon2) {
    return turf.booleanContains(polygon1, polygon2);
  }
  static ToUnion(polygon1, polygon2) {
    return turf.union(polygon1, polygon2);
  }
  static ToPolygon(geometry, props) {
    return turf.polygon(geometry, props);
  }
  static GetIntersect(polygon1, polygon2) {
    return turf.intersect(polygon1, polygon2);
  }
  static GetDifference(polygon1, polygon2) {
    return turf.difference(polygon1, polygon2);
  }

  
  static Check_Contain_Intersect2(target_polygon, element_polygon) {
    let is_contains_Crosses = false
    //判断是否包含
    is_contains_Crosses = TurfClass.CheckContains(target_polygon, element_polygon)//已经确定的多边形完全包含 新的多边形
    if (!is_contains_Crosses) {
      is_contains_Crosses = TurfClass.CheckContains(element_polygon, target_polygon)//新的多边形 包含  已经确定的多边形完全
    }
    //判断是否相交
    if (!is_contains_Crosses) {
      is_contains_Crosses = TurfClass.CheckIntersect(target_polygon, element_polygon)///已经确定的多边形完全与新的多边形相交
    }
    return is_contains_Crosses;
  }
  static PointInPolygon(poly_coordinates, lon, lat) {
    let pt = turf.point([lon - 0, lat - 0]);
    let poly = turf.polygon(poly_coordinates);
    return turf.booleanPointInPolygon(pt, poly);
  }
  static split_tifs(fcs) {
    var union = undefined
    for (let index = 0; index < fcs.length; index++) {
      let element = fcs[index];
      let element_polygon = TurfClass.ToPolygon(element.geometry.coordinates, element?.properties ? element?.properties : {})
      if (!union) {
        union = element_polygon
      }
      else {
        union = turf.union(union, element_polygon);
      }
    }
    let all_splits = []
    let type = union?.geometry?.type
    if (type == "MultiPolygon") { //多个区域网
      let union_coordinates = union.geometry.coordinates
      for (let index = 0; index < union_coordinates.length; index++) {
        let one_polygon_coordinates = union_coordinates[index];
        let target_polygon = TurfClass.ToPolygon(one_polygon_coordinates, {
          name: `${index + 1}`
        })
        let one_polygon_array = {
          vals: []
        }
        for (let index = 0; index < fcs.length; index++) {
          let one_fc = fcs[index];
          let element_polygon = TurfClass.ToPolygon(one_fc.geometry.coordinates, one_fc.properties)
          let is_contains_Crosses = TurfClass.Check_Contain_Intersect2(target_polygon, element_polygon)
          if (is_contains_Crosses) {
            one_polygon_array.vals.push(one_fc)
          }
        }
        all_splits.push(one_polygon_array)
      }
    }
    else {
      all_splits.push({
        vals: fcs,
      })
    }
    return all_splits;
  }
}

export function turf_set() {
  global["vapp"].config.globalProperties.$turf = TurfClass
}